<template>
  <el-dialog
    :visible="visible"
    :title="remarkTitleInfo"
    width="60%"
    @close="handleClose"
  >
    <div class="container">
      <div class="blocks" style="width: 100%">
        <el-form
          @submit.prevent.native
          ref="remarkForm"
          :model="form"
          label-width="150px"
        >
          <el-form-item :label="$t('common.remark') + ':'" prop="desc">
            <el-input
              v-model="form.desc"
              type="textarea"
              maxlength="128"
              show-word-limit
              :placeholder="$t('common.remark')"
              :rows="5"
            />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" @click="handleClose()">{{
          $t('page.cancel')
        }}</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="doOperation()"
          >{{ $t('common.confirm') }}</el-button
        >
      </span></template
    >
  </el-dialog>
</template>

<script>
import { addCmd } from '@/api/batch'
export default {
  props: {
    visible: Boolean,
    remarkTitleInfo: String
  },
  data() {
    return {
      batchId: '',
      form: {
        desc: ''
      },
      loading: false
    }
  },
  mounted() {
    this.batchId = this.$route.params.batchId
  },
  methods: {
    handleClose() {
      this.$refs.remarkForm.resetFields()
      this.$emit('close')
    },
    doOperation() {
      this.loading = true
      var params = {}
      switch (this.remarkTitleInfo) {
        case this.$t('command.A9.title'):
          params = {
            cmdTypeCode: 'A9',
            cmdDesc: this.form.desc
            // appPackageName: 'com.zhiyuan.app.ui', //传什么
            // param1: '2021-11-03 10:49:53', // 传什么
          }
          addCmd(this.batchId, params)
            .then(() => {
              this.$message({
                message: this.$t('batch.send_command_success'),
                type: 'success',
                onClose: () => {
                  this.handleClose()
                }
              })
            })
            .catch(this.$confirmError)
          break
        case this.$t('command.A8.title'):
          params = {
            cmdTypeCode: 'A8',
            cmdDesc: this.form.desc
          }
          addCmd(this.batchId, params)
            .then(() => {
              this.$message({
                message: this.$t('batch.send_command_success'),
                type: 'success',
                onClose: () => {
                  this.handleClose()
                }
              })
            })
            .catch(this.$confirmError)
            .finally(() => {
              this.loading = false
            })
          break
        case this.$t('command.B3.title'):
          params = {
            cmdTypeCode: 'B3',
            cmdDesc: this.form.desc
          }
          addCmd(this.batchId, params)
            .then(() => {
              this.$message({
                message: this.$t('batch.send_command_success'),
                type: 'success',
                onClose: () => {
                  this.handleClose()
                }
              })
            })
            .catch(this.$confirmError)
            .finally(() => {
              this.loading = false
            })
          break
        case this.$t('command.B1.title'):
          params = {
            cmdTypeCode: 'B1',
            cmdDesc: this.form.desc
          }
          addCmd(this.batchId, params)
            .then(() => {
              this.$message({
                message: this.$t('batch.send_command_success'),
                type: 'success',
                onClose: () => {
                  this.handleClose()
                }
              })
            })
            .catch(this.$confirmError)
            .finally(() => {
              this.loading = false
            })
          break
        case this.$t('command.desc.B2'):
          params = {
            cmdTypeCode: 'B2',
            cmdDesc: this.form.desc
          }
          addCmd(this.batchId, params)
            .then(() => {
              this.$message({
                message: this.$t('batch.send_command_success'),
                type: 'success',
                onClose: () => {
                  this.handleClose()
                }
              })
            })
            .catch(this.$confirmError)
            .finally(() => {
              this.loading = false
            })
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  height: 86px;
}
</style>
